﻿using System;
using System.Data;
using System.Data.OleDb;
using DDotNet.ORM.DataFactory;
using DDotNet.ORM.DataFactory.DirectAccess;
using DDotNet.ORM.DataFactory.DirectAccess.Enum;
using DDotNet.ORM.DataFactory.DirectAccess.QueryParameter;
using DDotNet.ORM.DataFactory.DirectAccess.SortParameter;
using DDotNet.ORM.DataFactory.Enum;
using DDotNet.ORM.DataFactory.Interface;
using GenericTests.Entities;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace GenericTests
{
    /// <summary>
    /// Summary description for Query
    /// </summary>
    [TestClass]
    public class Query
    {
        public Query()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        #region Additional test attributes

        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //

        #endregion

        private int _CurrentID;

        [TestInitialize()]
        public void Init()
        {
            // Cria Objetos
            User newUser = new User();
            ICrudFactory factory;

            // Coloca Valores
            newUser.Login = "LoginXYZ";
            newUser.Password = "1593578246";
            newUser.FullName = "FullName de Teste da Silva";
            newUser.Sex = SexEnumerator.Masc;

            try
            {
                using (factory = new CRUDFactory())
                {
                    // Insere
                    factory.Update(newUser);
                }

                // Recupera
                this._CurrentID = newUser.IDUser;
            }
            catch (Exception e)
            {
                Assert.Fail(e.InnerException.Message);
            }
        }

        [TestMethod]
        public void SelectAllWithOrderBy()
        {
            // Cria Objetos
            ICrudFactory factory;
            UserCollection users = new UserCollection();
            DirectAccessQueryFactory directAccessQueryFactory;

            using (factory = new CRUDFactory())
            {
                directAccessQueryFactory = new DirectAccessQueryFactory(factory);
                
                // Realiza Consulta
                users.Fill(
                directAccessQueryFactory.SelectAll(typeof(User), new GenericSortParameter("FullName", GenericSortMode.Asc))
                );
            }
        }

        [TestMethod]
        public void SelectAllWithLikeQuery()
        {
            // Cria Objetos
            ICrudFactory factory;
            DirectAccessQueryFactory directAccessQueryFactory;

            using (factory = new CRUDFactory())
            {
                directAccessQueryFactory = new DirectAccessQueryFactory(factory);
                
                // Realiza Consulta
                directAccessQueryFactory.SelectByQuery(typeof(User),
                                      new TextQueryParameter("FullName", TextQueryComparerMode.FullLike, "Full"));
            }
        }

        [TestMethod]
        public void SelectAllWithGenericQueryParameter()
        {
            // Cria Objetos
            ICrudFactory factory;
            DirectAccessQueryFactory directAccessQueryFactory;

            using (factory = new CRUDFactory())
            {
                directAccessQueryFactory = new DirectAccessQueryFactory(factory);
                
                // Realiza Consulta
                directAccessQueryFactory.SelectByQuery(typeof (User), new GenericBeetweenQueryParameter("IDUser", 0, 1024));
            }
        }
    }
}